package co.com.smart.persistence.entity;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import co.com.smart.persistence.entity.dto.SmartDatosMaestro;
import co.com.smart.persistence.entity.util.AbstractFacade;

/**
 * Session Bean implementation class DatosMaestroFacade
 */
@Stateless(name = "DatosMaestroFacade",
		   mappedName = "ejb/DatosMaestroFacade")
public class DatosMaestroFacade extends AbstractFacade<SmartDatosMaestro> implements DatosMaestroFacadeLocal {

	@PersistenceContext(unitName = "SmartServicesPU")
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public DatosMaestroFacade() {
       super(SmartDatosMaestro.class);
    }

    @Override
    protected EntityManager getEntityManager() {
    	return em;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<SmartDatosMaestro> filtrardatosmaestroxmaestro(SmartDatosMaestro entity) {
       	StringBuilder  sQuery = new StringBuilder("select c from SmartDatosMaestro c  ");
    		sQuery.append(" where c.smfhretiro is null ");
    		
    		if (entity.getSmartMaestro()  != null && entity.getSmartMaestro().getSmscmaestro() != null)
    		{
    			sQuery.append(" and c.smartMaestro = :sm_scmaestro ");			
    		}			

    		if(entity.getSmcousuario() != null && !"".equals(entity.getSmcousuario().trim())){
    			sQuery.append(" and c.smcousuario = :sm_cousuario ");
    		}
    		
    		if(entity.getSmdsdatmaestro() != null && !"".equals(entity.getSmdsdatmaestro().trim())){
    			sQuery.append(" and c.smdsdatmaestro like :sm_dsdatmaestro ");
    		}
    		
    		Query query = em.createQuery(sQuery.toString() );
    		
    		if (entity.getSmartMaestro()  != null && entity.getSmartMaestro().getSmscmaestro() != null) {
    			query.setParameter("sm_scmaestro", entity.getSmartMaestro());	
    		}
    		
    		if(entity.getSmcousuario() != null && !"".equals(entity.getSmcousuario().trim())) {
    			query.setParameter("sm_cousuario", entity.getSmcousuario().trim());
    		}
    		
    		if(entity.getSmdsdatmaestro() != null && !"".equals(entity.getSmdsdatmaestro().trim())) {
    			query.setParameter("sm_dsdatmaestro", entity.getSmdsdatmaestro().trim());
    		}
    		
    		return query.getResultList();	
    }
}